package spring.dao;

import db.domain.AskAccount;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smallframework.spring.DaoTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import sf.codegen.EntityEnhancerJavassist;
import sf.database.template.enjoy.EnjoyHandler;
import sf.tools.IOUtils;
import sf.tools.codec.HexUtils;
import spring.BaseServiceTest;

import java.io.File;
import java.io.IOException;


@Disabled
public class AskdaoDAO extends BaseServiceTest {
    static {
        // new EntityEnhancer().enhanceJavassist("sf.db.domain");
        just();
    }

    //	@BeforeClass
    public static void just() {
        new EntityEnhancerJavassist().enhance("db.domain");
    }

    static Logger log = LoggerFactory.getLogger(AskdaoDAO.class);

    @Autowired
    private DaoTemplate dt;

    //  @BeforeClass
    public static void before() {
        EnjoyHandler.getInstance().loadAllSQL();
    }

    /**
     * MD5方法
     * @param content
     * @return 密文
     * @throws Exception
     */
    public static String md5(byte[] content) {
        //加密后的字符串
        String encodeStr = DigestUtils.md5DigestAsHex(content);
        return encodeStr;
    }


    /**
     * 获取加密密码
     * @param user
     * @param orgPwd
     * @return
     */
    public static String getEncryptPwd(String user, String orgPwd) {
        String tempPwd = md5(orgPwd.getBytes());
        tempPwd = md5((user + tempPwd.toUpperCase() + "20070201").getBytes());
        return tempPwd.toUpperCase();
    }


    /**
     * 获取checksum
     * @param account
     * @param orgPwd
     * @return
     */
    public static String getCheckSum(AskAccount account, String orgPwd) {
        String encryptPwd = getEncryptPwd(account.getAccount(), orgPwd);
        StringBuilder sb = new StringBuilder();
        sb.append(account.getAccount());
        sb.append(encryptPwd);
        if (account.getPrivilege() == null) {
            account.setPrivilege(0);
        }
        if (account.getGoldCoin() == null) {
            account.setGoldCoin(0);
        }
        if (account.getSilverCoin() == null) {
            account.setSilverCoin(0);
        }
        sb.append(String.format("%08X", account.getPrivilege()));
        sb.append(String.format("%08X", account.getGoldCoin()));
        sb.append(String.format("%08X", account.getSilverCoin()));
        sb.append("ABCDEF");
        String str = sb.toString();
        return md5(str.getBytes()).toUpperCase();
    }

    @Test
    public void test1() {
        System.out.println(getEncryptPwd("atsh01", "123456"));
        AskAccount a = new AskAccount();
        a.setAccount("atsh01");
        a.setPrivilege(0);
        a.setGoldCoin(2000000000);
        a.setSilverCoin(2000000000);
        System.out.println(getCheckSum(a, "123456"));
    }

    @Test
    public void modifyJinnang() throws IOException {
        String fileName = "trump01";
        String name = "法宝";

        String sourcePath = "F:/baiduyundownload/askdao1.60/etc修改/gs/gs32/lib_gs32.pak/gs/clone/item/charge/jinnang.o";
        String targetPath = "C:/Users/ThinkPad/Desktop/非易充值注册/" + fileName + ".o";
        byte[] bytes = IOUtils.toByteArray(new File(sourcePath));

        byte[] jnBytes = "jinnang".getBytes();

        byte[] replaceBytes = "equip01".getBytes();

        for (int i = 0; i < bytes.length - 6; i++) {
            boolean look = true;
            for (int j = 0; j < jnBytes.length; j++) {
                if (bytes[i + j] != jnBytes[j]) {
                    look = false;
                    break;
                }
            }
            if (look) {
                for (int j = 0; j < replaceBytes.length; j++) {
                    bytes[i + j] = replaceBytes[j];
                }
            }
        }


        byte[] jncBytes = "锦囊".getBytes("GBK");

        byte[] replacecBytes = name.getBytes("GBK");

        for (int i = 0; i < bytes.length - 6; i++) {
            boolean look = true;
            for (int j = 0; j < jncBytes.length; j++) {
                if (bytes[i + j] != jncBytes[j]) {
                    look = false;
                    break;
                }
            }
            if (look) {
                for (int j = 0; j < replacecBytes.length; j++) {
                    bytes[i + j] = replacecBytes[j];
                }
            }
        }
        IOUtils.saveAsFile(new File(targetPath), bytes);

    }

    @Test
    public void modifyJinnangTest() throws IOException {


        byte[] jncBytes = "锦囊".getBytes("GBK");

        byte[] replacecBytes = "凡品".getBytes("GBK");
        System.out.println(new String(jncBytes));
        System.out.println(HexUtils.encodeHexString("锦囊".getBytes("UTF-8")).toUpperCase());


        System.out.println(HexUtils.encodeHexString(jncBytes).toUpperCase());
        System.out.println(HexUtils.encodeHexString(replacecBytes).toUpperCase());

        replacecBytes = "16饰".getBytes("GBK");
        System.out.println(HexUtils.encodeHexString(replacecBytes).toUpperCase());
    }

    @Test
    public void modifyJinnang2() throws IOException {
        String fileName = "trump01";
        String name = "法宝";

        String charset = "ISO-8859-1";

        String sourcePath = "F:/baiduyundownload/askdao1.60/etc修改/gs/gs32/lib_gs32.pak/gs/clone/item/charge/jinnang.o";
        String targetPath = "C:/Users/ThinkPad/Desktop/非易充值注册/" + fileName + ".o";
        byte[] bytes = IOUtils.toByteArray(new File(sourcePath));

        String str = new String(bytes, charset);
        str = str.replace("jinnang", fileName);

        byte[] jncBytes = "锦囊".getBytes("GBK");
        byte[] replacecBytes = name.getBytes("GBK");
        str = str.replace(new String(jncBytes, charset), new String(replacecBytes, charset));

        bytes = str.getBytes(charset);
        IOUtils.saveAsFile(new File(targetPath), bytes);

    }
}
